package cn.czu.wyw.config;

import cn.czu.wyw.intercept.UserTokenInterceptor;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author swg.
 * @Date 2020/4/1 22:15
 * @CONTACT 317758022@qq.com
 * @DESC
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    //实现静态资源的映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                //映射swagger2，否则会导致404
                .addResourceLocations("classpath:/META-INF/resources/")
                //这个就是映射的路径，下面的文件就可以通过localhost:8088/xxx.png来访问了
                .addResourceLocations("file:C:\\Users\\swg\\Desktop\\xiaoxuxushop\\images\\");
    }

    @Bean
    public RestTemplate restTemplateRe(RestTemplateBuilder builder){
        return builder.build();
    }

    @Bean
    public UserTokenInterceptor userTokenInterceptor(){
        return  new UserTokenInterceptor();
    }

    /**
     *  注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor( userTokenInterceptor())
               .addPathPatterns("/self/hello");
       WebMvcConfigurer.super.addInterceptors(registry);
    }
}
